<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
          name="cross"
          size="0.37333334rem"
          color="white"
          @click="backFn"
        />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span
          >我的频道
          <span class="small-title">
            点击{{ isShowCross ? "删除" : "进入" }}频道
          </span>
        </span>
        <span @click="editFn">{{ isShowCross ? "完成" : "编辑" }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="obj in checkPindao"
          @click="enterChannel(obj.id)"
          :key="obj.id"
        >
          <div class="channel-item van-hairline--surround">
            {{ obj.name }}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-show="isShowCross && !obj.id == 0"
              @click.native.stop="delFn(obj.id)"
            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                />
                <!-- @click.native="delFn(obj.id)"原生的没有点击事件 -->
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="obj in unCheckChannelList"
          :key="obj.id"
          @click="addFn(obj)"
        >
          <div class="channel-item van-hairline--surround">{{ obj.name }}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getChannelListAPI } from '@/api'
export default {
  data () {
    return {
      allChannelList: [], // 用于接收所有频道25个
      isShowCross: false // 点击编辑默认是false
    }
  },
  props: {
    checkPindao: Array // 点加号进来让用户看见已选频道(从父亲那里获得频道11个)
  },
  methods: {
    // 返回按钮X(弹出层关闭)
    backFn () {
      this.$emit('back', false) // 传一个布尔值给父亲
      this.isShowCross = false
    },
    // 点击编辑事件
    editFn () {
      if (this.checkPindao.id !== 0) {
        this.isShowCross = !this.isShowCross
      }
    },

    // 增加文章(增加到用户已经挑选的文章中)
    addFn (channelObj) {
      // 把文章id传下来,子传父实现
      // 如果用户点击的是编辑,才能把频道添加上去
      if (this.isShowCross === true) {
        this.$emit('addChannelEvent', channelObj) // 文章都是作为对象传入
      }
    },
    // 删除频道Id
    delFn (channelId) {
      if (channelId !== 0) {
        // 交给父亲去实现
        this.$emit('delChannelEvent', channelId)
      }
    },
    // 进入频道,让他显示在导航栏中
    enterChannel (channelId) {
      this.$emit('back', false) // 关闭弹出层
      this.$emit('input', channelId)
    }
  },
  // 获取用户所有频道
  async created () {
    console.log(this.checkPindao)
    const res = await getChannelListAPI()
    console.log(res)
    this.allChannelList = res.data.data.channels
  },

  computed: {
    // 过滤后的
    unCheckChannelList () {
      // 用于过滤重复的频道,smallObj代表用于选择的,剩下的代表没有选择
      // 遍历到更多频道列表即可
      return this.allChannelList.filter((obj) => {
        const index = this.checkPindao.findIndex(
          (smallObj) => smallObj.id === obj.id
        )
        if (index > -1) {
          return false
        } else {
          return true
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
